// App Root
var appPath = getAppPath();

// 是否延迟加载
var lazy = false;

// Datagrid ID
var datagrid_id = "#sm_moduleGrid";

//Datagrid URL参数
var datagrid_url = appPath+'/cms/staticManage_cascadePageModules.do';

// Dialog ID
var dialog_id = "#optionalDlg";

/**
 * jQuery页面加载事件
 */
$(function(){
	initModulePlugin();
});

/**
 * Datagrid控件初始化方法
 */
function initModulePlugin() {
	$(datagrid_id).datagrid({
		url: lazy? '' : datagrid_url,
		height: "full",
		pageSize: 10,
		striped: true,
		remoteSort:false,
		queryParams: getQueryParam(),
		idField:'moduleId',
		checkbox:true,
		frozenColumns:[[
		      {field:'ck',checkbox:true},
		      {field:'moduleId',title:'操作',width:40,align:'center',
		    	  formatter:function(value,rowData,rowIndex){
		    	  	  //var dataStr = JSON.stringify(rowData);
		    		  return "<a href='javascript:void(0)'><img border='0' width='12px' height='12px'  src='"+appPath+"/js/lib/jquery-easyui/themes/icons/edit_add.png' title='新增展示数据' onClick='showChooseData("+rowIndex+")'/></a>";
		    	  }
		      }
		]],
		columns:[[
		      {field:"moduleName",title:'模块名称',width:150,align:'center',sortable:true},
		      {field:'moduleCode',title:'模块代码',width:120,align:'center'},
		      {field:'typeName',title:'类型',width:100,align:'center'},
		      {field:'remark',title:'备注',width:200,align:'left'}
		 ]],
		 pagination:true,
		 rownumbers:true,
		 onLoadSuccess: function() {
			 uncheckAll(this);
			 changeTipInfo("tommy-cell");
		 },
		 toolbar:[{
			id:'btnback',
			text:'返回',
			iconCls:'icon-undo',
			handler:function(){
				$.loading(true);
				window.location.replace(appPath + "/jsp/cms/pageStyle.jsp");
			}
		}]
	});
	if (lazy)
		$(datagrid_id).datagrid('options').url = datagrid_url;
}

/**
 * 控件展现方法
 */
function showPlugin() {
	if (lazy) {
		initPlugin();
		lazy = false;
	}		
}

/**
 * 获取请求信息
 * @return {}
 */
function getQueryParam(){
	var submitObject = new Object();
	submitObject["pageModule.pageStyleCd"] = s_pageStyleCd;
	return submitObject;
}

/**
 * 展现可选择数据页面
 * @param {} rowData
 */
function showChooseData(rowIndex){
	var rowData = $(datagrid_id).datagrid("getRows")[rowIndex];
	$(dialog_id).data("typeId", rowData.typeId);
	$(dialog_id).data("typeName", rowData.typeName);
	switch(rowData.moduleStyle) {
	case 1:
		initInfoPlugin();
		initSearchItemForOptionalInfo();
		break;
	case 2:
		initAdvPlugin();
		initSearchItemForOptionalAdv();
		break;	
	default:
		$.messager.alert('提示信息','未定义的模块类型！','info');
	}
	showDataWindow(dialog_id, rowData.typeName + "列表");
}

/**
 * 弹出数据展示窗口
 */
function showDataWindow(dlgId, title, icon) {
	$(dlgId).window({
		title:title,
		iconCls:(icon != undefined && icon != null? icon : 'icon-exportExcel'),
		modal: true,
		shadow: true,
		collapsible:false,
		minimizable:false,
		maximizable:false,
		onClose: dlgId != dialog_id? doReloadOptional : function(){}
	});
	$(dlgId).window('move',{top:80});
	$(dlgId).window('open');
}

/**
 * 关闭Form对话框
 */
function closeLayoutWindow(){
	$('#editLayoutDlg').window('close');
}

/**
 * 显示当前列表相关搜索项
 */
function showRelatedSearchItem(table_id) {
	$(table_id).show().siblings(".searchItem").hide();
}

/**
 * 重载可选数据grid，取消全选
 */
function doReloadOptional(type){
	var ops = $('#sm_optionalGrid').datagrid('getPager').pagination("options");
	if(type == "save"){
		var pn = Math.floor(ops.total/ops.pageSize)+1;
		$('#sm_optionalGrid').datagrid('getPager').pagination({pageNumber:pn});
		$('#sm_optionalGrid').datagrid('options').pageNumber = pn;
	}else if(type == "delete"){
		if($('#sm_optionalGrid').datagrid('getRows').length == $('#sm_optionalGrid').datagrid('getSelections').length){
			var pn ;
			var pnt = Math.ceil(ops.total/ops.pageSize);
			var pm = ops.pageNumber;
			if(pnt == (pm-1)){
				pn = pnt ;
			}else{
				pn = pm
			}
			$('#sm_optionalGrid').datagrid('getPager').pagination({pageNumber:(pn==0?1:pn)});
			$('#sm_optionalGrid').datagrid('options').pageNumber = (pn==0?1:pn);
		}
	}
	$("#sm_optionalGrid").datagrid('unselectAll');
	$("#sm_optionalGrid").datagrid('reload');
}

/**
 * 重载关联数据grid，取消全选
 */
function doReloadCascade(type){
	var ops = $('#sm_cascadeGrid').datagrid('getPager').pagination("options");
	if(type == "save"){
		var pn = Math.floor(ops.total/ops.pageSize)+1;
		$('#sm_cascadeGrid').datagrid('getPager').pagination({pageNumber:pn});
		$('#sm_cascadeGrid').datagrid('options').pageNumber = pn;
	}else if(type == "delete"){
		if($('#sm_cascadeGrid').datagrid('getRows').length == $('#sm_cascadeGrid').datagrid('getSelections').length){
			var pn ;
			var pnt = Math.ceil(ops.total/ops.pageSize);
			var pm = ops.pageNumber;
			if(pnt == (pm-1)){
				pn = pnt ;
			}else{
				pn = pm
			}
			$('#sm_cascadeGrid').datagrid('getPager').pagination({pageNumber:(pn==0?1:pn)});
			$('#sm_cascadeGrid').datagrid('options').pageNumber = (pn==0?1:pn);
		}
	}
	$("#sm_cascadeGrid").datagrid('unselectAll');
	$("#sm_cascadeGrid").datagrid('reload');
}
